﻿
namespace Huirui.Cavan.Presentation.Seedwork
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.Contracts;
    using System.Linq;
    using Huirui.Cavan.Core.Message;
    using Huirui.Cavan.Core.Extension;
    using Huirui.Cavan.Core.Persistent;

    /// <summary>
    ///     Provides Models based on interfaces
    /// </summary>
    public static class ModelFactory
    {
        const string CannotCreateMessage = "Cannot create IModel with 'null' instead of IList<Message> 'messages'";

        /// <summary>
        ///     Creates the IModel and fills its Messages with supplied collection
        /// </summary>
        /// <typeparam name = "T"></typeparam>
        /// <param name = "messages">must be not null</param>
        /// <returns></returns>
        public static T CreateModel<T>(IList<AppMessage> messages)
            where T : ICoreModel
        {
            Contract.Requires(messages.Is(), CannotCreateMessage);
            var model = FactoryProvider.CreateInstance<T>(messages);
            return model;
        }

        public static T CreateModel<T>()
            where T : ICoreModel
        {
            var model = FactoryProvider.CreateInstance<T>(new List<AppMessage>());
            return model;
        }

        public static T CreateSearch<T>()
            where T : ISearch
        {
            var model = FactoryProvider.CreateInstance<T>(null);
            return model;
        }
    }
}